//
//  NYTopicViewModel.m
//  Nooryes
//
//  Created by baken on 2020/3/3.
//  Copyright © 2020 MyApp. All rights reserved.
//

#import "NYTopicViewModel.h"
#import "NYTopicManager.h"
#import "NYTopicListModel.h"
#import "NYArticleListModel.h"
#import "NYTopicDetailModel.h"
#import "NYAccountModel.h"
#import "NYRecommendManager.h"

@interface NYTopicViewModel()

@property(nonatomic, assign) NSInteger articlePage;

@end

@implementation NYTopicViewModel



-(void)getMoreDataCompletionHandle:(CompletionHandle)completionHandle{
    self.pageNum++;
    [self getTopicListWithCompletionHandle:completionHandle];
}
- (void)refreshDataCompletionHandle:(CompletionHandle)completionHandle{
    self.pageNum = 1;
    [self getTopicListWithCompletionHandle:completionHandle];
}


- (void)getTopicListWithCompletionHandle:(CompletionHandle)complete{
    [NYTopicManager getTopicListWithPage:STR_FROM_INT(self.pageNum) andCompletionHandle:^(NYTopicListModel * _Nonnull model, NSError * _Nonnull error) {
        if (!error) {
            if (self.pageNum == 1) {
                [self.dataArr removeAllObjects];
            }
            [self.dataArr addObjectsFromArray:model.data];
        }
        complete(error);
    }];
}


- (void)refreshArticlesCompletionHandle:(CompletionHandle)completionHandle{
    self.articlePage = 1;
    [self getArticlesListWithCompletionHandle:completionHandle];
}


-(void)getMoreArticlesCompletionHandle:(CompletionHandle)completionHandle{
    self.articlePage++;
    [self getArticlesListWithCompletionHandle:completionHandle];
}

- (void)getArticlesListWithCompletionHandle:(CompletionHandle)completionHandle{
    [NYTopicManager getTopicArticleWithTopicID:self.topicID andPage:STR_FROM_INT(self.articlePage) andOrder:self.order andCompletionHandle:^(NYArticleListModel * _Nonnull model, NSError * _Nonnull error) {
        if (!error) {
            if (self.articlePage == 1) {
                [self.articlesArray removeAllObjects];
            }
            [self.articlesArray addObjectsFromArray:model.data];
        }
        completionHandle(error);
    }];
}

- (void)getTopicDetailWithCompletionHandle:(CompletionHandle)complete{
    [NYTopicManager getTopicDetailWithTopicID:self.topicID andCompletionHandle:^(NYTopicDetailModel * _Nonnull model, NSError * _Nonnull error) {
        if (!error) {
            self.deatil = model.data;
        }
        complete(error);
    }];
}

-(NSMutableArray *)articlesArray{
    if (!_articlesArray) {
        _articlesArray = [NSMutableArray array];
    }
    return _articlesArray;
}


- (void)getOtherUserInfoWithCompletionHandle:(CompletionHandle)complete{
    [NYTopicManager getOtherUserInfoWithUserID:self.userid andCompletionHandle:^(NYAccountModel * model, NSError * error) {
        if (!error) {
            self.otherUser = model.data;
        }
        complete(error);
    }];
}

- (void)getOtherUserArticlesInfoWithCompletionHandle:(CompletionHandle)complete{
    [NYTopicManager getOtherUserArticlesWithUserID:self.userid andPage:STR_FROM_INT(self.articlePage) andCompletionHandle:^(NYArticleListModel * model, NSError * error) {
        if (!error) {
            if (self.articlePage == 1) {
                [self.articlesArray removeAllObjects];
            }
            [self.articlesArray addObjectsFromArray:model.data];
        }
        complete(error);
    }];
}


- (void)refreshOtherArticlesCompletionHandle:(CompletionHandle)completionHandle{
    self.articlePage = 1;
    [self getOtherUserArticlesInfoWithCompletionHandle:completionHandle];
}


-(void)getMoreOtherArticlesCompletionHandle:(CompletionHandle)completionHandle{
    self.articlePage++;
    [self getOtherUserArticlesInfoWithCompletionHandle:completionHandle];
}


- (void)collectArticleWithCompletionHandle:(CompletionHandle)complete{
    NSString *uid = [[NSUserDefaults standardUserDefaults]objectForKey:@"userid"];
    NSString *sessionid = [[NSUserDefaults standardUserDefaults]objectForKey:@"sessionid"];
    if (self.deatil.iscollect) {
        [NYRecommendManager cancelCollectInfoWithUserID:uid andSessionID:sessionid andType:@"2" andID:self.topicID andCompletionHandle:^(NYMainModel *model, NSError * error) {
            self.deatil.iscollect = NO;
            complete(error);
        }];
    } else {
        [NYRecommendManager collectInfoWithUserID:uid andSessionID:sessionid andType:@"2" andID:self.topicID andCompletionHandle:^(NYMainModel *model, NSError * error) {
             self.deatil.iscollect = YES;
            complete(error);
        }];
    }
}


@end
